<?php
namespace Api\Controller;

use Think\Controller;

class BankcardController extends CommonController
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 银行卡列表
     * @return [type] [description]
     */
    public function index()
    {
        $bank_card = D('Api/Bankcard', 'Service')->lists(USER_ID);
        success($bank_card);
    }

    /**
     * 添加银行卡
     */
    public function add()
    {
        $data = [
            'name'   => $name = I('post.name', '', 'trim'),
            'number' => $number = I('post.number', '', 'trim'),
            'type'   => $type = I('post.type', 1, 'intval'),
        ];

        if (!in_array($type, [1, 2])) {
            error(8015, '银行卡类型错误');
        }

        $res = D('Api/Bankcard', 'Service')->add(USER_ID, $data);

        if ($res) {
            success();
        } else {
            error(8016, '添加银行卡失败');
        }
    }

    /**
     * 删除银行卡
     * @return [type] [description]
     */
    public function delete()
    {
        $id = I('post.id', 1, 'intval');

        $res = D('Api/Bankcard', 'Service')->delete(USER_ID, $id);

        if ($res) {
            success();
        } else {
            error(8017, '删除银行卡失败');
        }
    }
}
